#include "Maintask.h"

extern GUI_CONST_STORAGE GUI_BITMAP bmCeditor_72px;
extern GUI_CONST_STORAGE GUI_BITMAP bmCfile_93;
extern GUI_CONST_STORAGE GUI_BITMAP bmCHelp72px;
extern GUI_CONST_STORAGE GUI_BITMAP bmCjoystick;
extern GUI_CONST_STORAGE GUI_BITMAP bmCRun_72px;
extern GUI_CONST_STORAGE GUI_BITMAP bmCsettings;
extern GUI_CONST_STORAGE GUI_BITMAP bmPeditor_72px;
extern GUI_CONST_STORAGE GUI_BITMAP bmPfile_93px;
extern GUI_CONST_STORAGE GUI_BITMAP bmPHelp;
extern GUI_CONST_STORAGE GUI_BITMAP bmPjoystick;
extern GUI_CONST_STORAGE GUI_BITMAP bmPRun;
extern GUI_CONST_STORAGE GUI_BITMAP bmPsettingS;

extern WM_HWIN CreateRunwin(void);
extern WM_HWIN CreateEitorwin(void);
extern WM_HWIN CreateHandlewin(void);
extern WM_HWIN CreateFilewin(WM_HWIN himt);
extern WM_HWIN CreateSetwin(void);
extern WM_HWIN CreateHelpwin(void);
/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_WINDOW_0     (GUI_ID_USER + 0x00)
#define ID_BUTTON_0     (GUI_ID_USER + 0x03)
#define ID_BUTTON_1     (GUI_ID_USER + 0x04)
#define ID_BUTTON_2     (GUI_ID_USER + 0x05)
#define ID_BUTTON_4     (GUI_ID_USER + 0x07)
#define ID_BUTTON_5     (GUI_ID_USER + 0x08)

#define ID_TEXT_0       (GUI_ID_USER + 0x09)
#define ID_TEXT_1       (GUI_ID_USER + 0x0a)
#define ID_TEXT_2       (GUI_ID_USER + 0x0b)
#define ID_TEXT_4       (GUI_ID_USER + 0x0d)
#define ID_TEXT_5       (GUI_ID_USER + 0x0e)
// USER START (Optionally insert additional defines)
char *_acbuffer = NULL;
UINT    f_num;
extern FATFS fs;//逻辑磁盘工作区.
FIL img_file;
FRESULT result; 
DIR     dir;

/****界面标志********/
uint8_t runWin_flag = 0;
uint8_t editWin_flag = 0;
uint8_t handleWin_flag = 0;
uint8_t setWin_flag = 0;
/*********************************************************************
*
*       _aDialogCreate
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] =
{
    { WINDOW_CreateIndirect, "Window", ID_WINDOW_0, 0, 0, 800, 480, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "", ID_BUTTON_0, 150, 150, 72, 72, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "", ID_BUTTON_1, 230, 150, 72, 72, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "", ID_BUTTON_2, 310, 150, 72, 72, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "", ID_BUTTON_4, 390, 150, 72, 72, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "", ID_BUTTON_5, 470, 150, 72, 72, 0, 0x0, 0 },
	{ TEXT_CreateIndirect, "运行", ID_TEXT_0, 150, 72+150+16, 80, 20, 0, 0x0, 0 },
	{ TEXT_CreateIndirect, "编辑", ID_TEXT_1, 230, 72+150+16, 80, 20, 0, 0x0, 0 },
	{ TEXT_CreateIndirect, "手动", ID_TEXT_2, 310, 72+150+16, 80, 20, 0, 0x0, 0 },
	{ TEXT_CreateIndirect, "设置", ID_TEXT_4, 390, 72+150+16, 80, 20, 0, 0x0, 0 },
	{ TEXT_CreateIndirect, "帮助", ID_TEXT_5, 470, 72+150+16, 80, 20, 0, 0x0, 0 },
    // USER START (Optionally insert additional widgets)
    // USER END
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

static WM_HMEM hMem;
static GUI_MEMDEV_Handle hBMP;

// USER START (Optionally insert additional static code)

/**
  * @brief 使用内存设备加载BMP图片数据
  * @note wu
  * @param sFilename：需要加载的图片名
  * @retval 无
  */
static GUI_HMEM LoadBMP_UsingMEMDEV(const char *sFilename)
{
	/* 打开图片 */
	result = f_open(&img_file, sFilename, FA_OPEN_EXISTING | FA_READ);
	if ((result != FR_OK))
	{
		printf("文件打开失败！\r\n");
		_acbuffer[0]='\0';
	}
	
	/* 申请一块动态内存空间 */
	hMem = GUI_ALLOC_AllocZero(f_size(&img_file));
	/* 转换动态内存的句柄为指针 */
	_acbuffer = GUI_ALLOC_h2p(hMem);

	/* 读取图片数据到动态内存中 */
	result = f_read(&img_file, _acbuffer, f_size(&img_file), &f_num);
	if(result != FR_OK)
	{
		printf("文件读取失败！\r\n");
	}
	/* 读取完毕关闭文件 */
	f_close(&img_file);
	/* 创建内存设备 */
	hBMP = GUI_MEMDEV_Create(0, 0,                        /* 起始坐标 */
													 GUI_BMP_GetXSize(_acbuffer), /* x方向尺寸 */
													 GUI_BMP_GetYSize(_acbuffer));/* y方向尺寸 */
	/* 选择内存设备 */
	GUI_MEMDEV_Select(hBMP);
	/* 绘制BMP到内存设备中 */
	GUI_BMP_Draw(_acbuffer, 0, 0);
	/* 切换内存设备到LCD */
	GUI_MEMDEV_Select(0);
	/* 释放内存 */
	GUI_ALLOC_Free(hMem);
	
	return hBMP;
}

// USER END

/*********************************************************************
*
*       _cbDialog
*/
static void _cbDialog(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    int     NCode;
    int     Id;
    // USER START (Optionally insert additional variables)
    // USER END

    switch (pMsg->MsgId)
    {
        case WM_INIT_DIALOG:
            //
            // Initialization of 'Window'
            //
            hItem = pMsg->hWin;
            WINDOW_SetBkColor(hItem, GUI_BLACK);
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_0);
			BUTTON_SetSkinClassic(hItem);
			BUTTON_SetFrameColor(hItem,GUI_TRANSPARENT);
			BUTTON_SetBkColor(hItem,BUTTON_CI_UNPRESSED, GUI_TRANSPARENT);
			BUTTON_SetBkColor(hItem,BUTTON_CI_PRESSED, GUI_TRANSPARENT);
			BUTTON_SetBitmap(hItem, BUTTON_BI_UNPRESSED, &bmPRun);
			BUTTON_SetBitmap(hItem, BUTTON_BI_PRESSED, &bmCRun_72px);
		
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_1); //编辑按钮设置
			BUTTON_SetSkinClassic(hItem);
			BUTTON_SetBkColor(hItem,BUTTON_CI_UNPRESSED, GUI_TRANSPARENT);
			BUTTON_SetBkColor(hItem,BUTTON_CI_PRESSED, GUI_TRANSPARENT);
			BUTTON_SetBitmap(hItem, BUTTON_BI_UNPRESSED, &bmPeditor_72px);
			BUTTON_SetBitmap(hItem, BUTTON_BI_PRESSED, &bmCeditor_72px);
			BUTTON_SetFrameColor(hItem,GUI_TRANSPARENT);
		
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_2); //操作按钮设置
			BUTTON_SetSkinClassic(hItem);
			BUTTON_SetBkColor(hItem,BUTTON_CI_UNPRESSED, GUI_TRANSPARENT);
			BUTTON_SetBkColor(hItem,BUTTON_CI_PRESSED, GUI_TRANSPARENT);
			BUTTON_SetBitmap(hItem, BUTTON_BI_UNPRESSED, &bmPjoystick);
			BUTTON_SetBitmap(hItem, BUTTON_BI_PRESSED, &bmCjoystick);
			BUTTON_SetFrameColor(hItem,GUI_TRANSPARENT);
			
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_4); //设置按钮设置
			BUTTON_SetSkinClassic(hItem);
			BUTTON_SetBkColor(hItem,BUTTON_CI_UNPRESSED, GUI_TRANSPARENT);
			BUTTON_SetBkColor(hItem,BUTTON_CI_PRESSED, GUI_TRANSPARENT);
			BUTTON_SetBitmap(hItem, BUTTON_BI_UNPRESSED, &bmPsettingS);
			BUTTON_SetBitmap(hItem, BUTTON_BI_PRESSED, &bmCsettings);
			BUTTON_SetFrameColor(hItem,GUI_TRANSPARENT);
			
			hItem = WM_GetDialogItem(pMsg->hWin, ID_BUTTON_5); //帮助按钮设置
			BUTTON_SetSkinClassic(hItem);
			BUTTON_SetBkColor(hItem,BUTTON_CI_UNPRESSED, GUI_TRANSPARENT);
			BUTTON_SetBkColor(hItem,BUTTON_CI_PRESSED, GUI_TRANSPARENT);
			BUTTON_SetBitmap(hItem, BUTTON_BI_UNPRESSED, &bmPHelp);
			BUTTON_SetBitmap(hItem, BUTTON_BI_PRESSED, &bmCHelp72px);
			BUTTON_SetFrameColor(hItem,GUI_TRANSPARENT);
			
			hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_0);
			TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
			TEXT_SetFont(hItem, &GUI_Font20);
			TEXT_SetTextColor(hItem,GUI_BLACK);
			hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_1);
			TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
			TEXT_SetFont(hItem, &GUI_Font20);
			TEXT_SetTextColor(hItem,GUI_BLACK);
			hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_2);
			TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
			TEXT_SetFont(hItem, &GUI_Font20);
			TEXT_SetTextColor(hItem,GUI_BLACK);
			hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_4);
			TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
			TEXT_SetFont(hItem, &GUI_Font20);
			TEXT_SetTextColor(hItem,GUI_BLACK);
			hItem = WM_GetDialogItem(pMsg->hWin, ID_TEXT_5);
			TEXT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
			TEXT_SetTextColor(hItem,GUI_BLACK);
			TEXT_SetFont(hItem, &GUI_Font20);
            break;
		case WM_PAINT:
			/* 将背景图片从内存设备写入LCD */
			GUI_MEMDEV_WriteOpaqueAt(hBMP, 0, 0);
		  break;
        case WM_NOTIFY_PARENT:
            Id    = WM_GetId(pMsg->hWinSrc);
            NCode = pMsg->Data.v;

            switch(Id)
            {
                case ID_BUTTON_0: // Notifications sent by 'Button'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
							beep_on();
                            break;

                        case WM_NOTIFICATION_RELEASED:
							beep_off();
							runWin_flag = 1;
							CreateRunwin();
                            break;
                    }

                    break;

                case ID_BUTTON_1: // Notifications sent by 'Button'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            beep_on();
                            break;

                        case WM_NOTIFICATION_RELEASED:
							beep_off();
							editWin_flag = 1;
							CreateEitorwin();            
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }

                    break;

                case ID_BUTTON_2: // Notifications sent by 'Button'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            beep_on();
                            break;

                        case WM_NOTIFICATION_RELEASED:
                            beep_off();
							handleWin_flag = 1;
							CreateHandlewin();
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }

                    break;

                case ID_BUTTON_4: // Notifications sent by 'Button'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            beep_on();
                            break;

                        case WM_NOTIFICATION_RELEASED:
                            beep_off();
							setWin_flag = 1;
							CreateSetwin();
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }

                    break;
                    // USER START (Optionally insert additional code for further Ids)
                    // USER END

				case ID_BUTTON_5: // Notifications sent by 'Button'
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_CLICKED:
                            beep_on();
                            break;

                        case WM_NOTIFICATION_RELEASED:
                            beep_off();
							CreateHelpwin();
                            break;
                            // USER START (Optionally insert additional code for further notification handling)
                            // USER END
                    }

                    break;
                    // USER START (Optionally insert additional code for further Ids)
                    // USER END
            }

            break;

        // USER START (Optionally insert additional message handling)
        // USER END
        default:
            WM_DefaultProc(pMsg);
            break;
    }
}


/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       CreateWindow
*/
WM_HWIN 	HomeWin;


void MainTask(void)
{
	WM_SetCreateFlags(WM_CF_MEMDEV);
	WM_EnableMemdev(WM_HBKWIN);
	GUI_UC_SetEncodeUTF8();
	printf("flash id is %x\r\n",W25QXX_ReadID());
	LoadBMP_UsingMEMDEV("1:/image/wallpaper.bmp");
    HomeWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, HDTWIN, 0, 0);

    while(1)
    {
		GUI_Delay(70);
    }
}

// USER START (Optionally insert additional public code)
// USER END

/*************************** End of file ****************************/
